package cn.ds.hbase.controller;

import cn.ds.hbase.entity.UserInfo;
import cn.ds.hbase.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目名称:hbase-learn<br>
 * 包名:cn.ds.hbase.controller<br>
 * 用于:hbase-learn<br>
 * 创建时间:2019年03月15日<br>
 * 更新时间:2019年03月15日<br>
 *
 * @author :lds（创建人）<br>
 * @version :v1.0（版本号）<br>
 * @since jdk1.8
 */
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @GetMapping("/get/id/{id}")
    public UserInfo getById(@PathVariable("id") Integer id){
        final UserInfo userInfo = userInfoMapper.getById(id);
        return userInfo;
    }

    @GetMapping("/get/name/{name}")
    public List<UserInfo> getByName(@PathVariable("name") String name){
        return userInfoMapper.getByName(name);
    }

    @GetMapping("/get/all")
    public List<UserInfo> getAll(){
        return userInfoMapper.getAll();
    }

    @PostMapping("/delete/id/{id}")
    public Integer deleteById(@PathVariable("id") Integer id){
        return userInfoMapper.deleteById(id);
    }

    @PostMapping("/add")
    public Integer addUser(@RequestBody UserInfo userInfo){
        final int i = userInfoMapper.addUser(userInfo);
        if (i==0){
            throw new RuntimeException("插入失败");
        }
        return userInfo.getId();
    }
}
